package com.woniuxy.joylifecenter.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.CenterRole;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.RoleService;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@Api(value = "", description = "角色管理Controller")
@Slf4j
public class Center_Role_Controller {

    @Autowired
    RoleService roleService;

    /**
     * @return
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param
     * @Description 添加角色
     **/
    @ApiOperation(value = "/addRole", notes = "添加角色")
    @RequestMapping("/addRole")
    @ResponseBody
    public VO addRole(@RequestBody String centerRoleName) {

        CenterRole centerRole = new CenterRole();
        centerRole.setCenterRoleid(UUID.randomUUID().toString().replaceAll("-", ""));
        centerRole.setCenterRoleName(centerRoleName);
        VO vo = new VO();
        int count = 0;
        try {
            count = roleService.addRole(centerRole);
            vo.setMsg("成功添加角色:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCount(count);
        vo.setCode(0);
        vo.setData(null);
        return vo;

    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [centerRole]
     * @Description 编辑角色
     **/
    @ApiOperation(value = "/editRole", notes = "编辑角色")
    @RequestMapping("/editRole")
    @ResponseBody
    public VO editRole(@RequestBody CenterRole centerRole) {
        VO vo = new VO();
        int count = 0;
        try {
            count = roleService.editRole(centerRole);
            vo.setMsg("本次更新了:");

        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCount(count);
        vo.setCode(0);
        vo.setData(null);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [roleId]
     * @Description 删除单个角色
     **/
    @ApiOperation(value = "/deleteRole", notes = "删除角色")
    @RequestMapping("/deleteRole")
    @ResponseBody
    public VO deleteRole(@RequestBody String roleId) {
        VO vo = new VO();
        int count = 0;
        try {
            count = roleService.deleteRole(roleId);
            vo.setMsg("已删除:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCode(0);
        vo.setCount(count);
        vo.setData(null);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [roleIds]
     * @Description 批量删除角色
     **/
    @ApiOperation(value = "/batchDelteRole", notes = "批量角色")
    @RequestMapping("/batchDeleteRole")
    @ResponseBody
    public VO batchDeleteRole(@RequestBody List<String> roleIds) {
        System.out.println("后端接收到的待批量删除数据：" + roleIds);
        VO vo = new VO();
        int count = 0;
        try {
            count = roleService.batchDeleteRole(roleIds);
            vo.setMsg("已批量删除:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCode(0);
        vo.setCount(count);
        vo.setData(null);
        return vo;
    }

    @ApiOperation(value = "/showRoleByCondition", notes = "多条件模糊查询角色")
    @RequestMapping("/showRoleByCondition")
    @ResponseBody
    public VO showRoleByCondition(@RequestBody HashMap<String, String> pageMap) {
        CenterRole centerRole = new CenterRole();

        String jsonstr = pageMap.get("jsonstr");
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = new HashMap<>();
        try {
            map = (HashMap<String, String>) mapper.readValue(jsonstr, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        centerRole.setCenterRoleid(map.get("centerRoleid"));
        centerRole.setCenterRoleName(map.get("centerRoleName"));
        PageHelper.startPage(Integer.parseInt(pageMap.get("page")), Integer.parseInt(pageMap.get("limit")));
        List<CenterRole> centerRoles = roleService.queryRoleByCondition(centerRole);
        PageInfo pageInfo = new PageInfo(centerRoles);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        vo.setData(pageInfos);
        //此处需要从pageInfo获取总记录条数，不能直接用Service返回的集合大小
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        return vo;

    }



    @ApiOperation(value = "/loadTree", notes = "加载权限树")
    @RequestMapping("/loadTree")
    @ResponseBody
    public VO loadTree() {
        List<String> json=new ArrayList<String>();
        json.add("[{\"menuId\":1,\"title\":\"后台首页\",\"icon\":\"\uE628\",\"href\":\"/page/main.html\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":0,\"sorting\":99991,\"checked\":null,\"isOpen\":false},{\"menuId\":2,\"title\":\"权限管理\",\"icon\":\"\uE612\",\"href\":\"\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":0,\"sorting\":999,\"checked\":null,\"isOpen\":false},{\"menuId\":9,\"title\":\"用户管理\",\"icon\":\"&#xe61d;\",\"href\":null,\"perms\":null,\"spread\":\"false\",\"parentId\":0,\"sorting\":998,\"checked\":null,\"isOpen\":false},{\"menuId\":51,\"title\":\"轮播管理\",\"icon\":\"\uE663\",\"href\":\"\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":0,\"sorting\":997,\"checked\":null,\"isOpen\":false},{\"menuId\":43,\"title\":\"SQL监控\",\"icon\":\"&#xe642;\",\"href\":null,\"perms\":null,\"spread\":\"false\",\"parentId\":0,\"sorting\":996,\"checked\":null,\"isOpen\":false},{\"menuId\":14,\"title\":\"系统日志\",\"icon\":\"&#xe61d;\",\"href\":null,\"perms\":null,\"spread\":\"false\",\"parentId\":0,\"sorting\":995,\"checked\":null,\"isOpen\":false},{\"menuId\":3,\"title\":\"角色管理\",\"icon\":\"\uE613\",\"href\":\"/sys/roleList\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":2,\"sorting\":99,\"checked\":null,\"isOpen\":false},{\"menuId\":4,\"title\":\"管理员列表\",\"icon\":\"&#xe613;\",\"href\":\"/sys/adminList\",\"perms\":null,\"spread\":\"false\",\"parentId\":2,\"sorting\":98,\"checked\":null,\"isOpen\":false},{\"menuId\":46,\"title\":\"菜单管理\",\"icon\":\"&#xe642;\",\"href\":\"/sys/menuList\",\"perms\":null,\"spread\":\"false\",\"parentId\":2,\"sorting\":97,\"checked\":null,\"isOpen\":false},{\"menuId\":71,\"title\":\"接口工具\",\"icon\":\"\",\"href\":\"/swagger-ui.html\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":0,\"sorting\":8,\"checked\":null,\"isOpen\":false},{\"menuId\":69,\"title\":\"多数据源\",\"icon\":\"\uE673\",\"href\":\"\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":0,\"sorting\":1,\"checked\":null,\"isOpen\":false},{\"menuId\":10,\"title\":\"添加用户\",\"icon\":\"&#xe608;\",\"href\":\"/user/addUser\",\"perms\":null,\"spread\":\"false\",\"parentId\":9,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":11,\"title\":\"管理用户\",\"icon\":\"&#xe60e;\",\"href\":\"/user/userList\",\"perms\":null,\"spread\":\"false\",\"parentId\":9,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":15,\"title\":\"日志管理\",\"icon\":\"\uE642\",\"href\":\"/log/logList\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":14,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":16,\"title\":\"查看\",\"icon\":\"\",\"href\":\"\",\"perms\":\"sys:role:list\",\"spread\":\"false\",\"parentId\":3,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":17,\"title\":\"新增\",\"icon\":null,\"href\":null,\"perms\":\"sys:role:save\",\"spread\":\"false\",\"parentId\":3,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":18,\"title\":\"修改\",\"icon\":null,\"href\":null,\"perms\":\"sys:role:update\",\"spread\":\"false\",\"parentId\":3,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":19,\"title\":\"删除\",\"icon\":null,\"href\":null,\"perms\":\"sys:role:delete\",\"spread\":\"false\",\"parentId\":3,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":20,\"title\":\"查看\",\"icon\":null,\"href\":null,\"perms\":\"sys:admin:list\",\"spread\":\"false\",\"parentId\":4,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":21,\"title\":\"新增\",\"icon\":null,\"href\":null,\"perms\":\"sys:admin:save\",\"spread\":\"false\",\"parentId\":4,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":22,\"title\":\"修改\",\"icon\":null,\"href\":null,\"perms\":\"sys:admin:update\",\"spread\":\"false\",\"parentId\":4,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":23,\"title\":\"删除\",\"icon\":null,\"href\":null,\"perms\":\"sys:admin:delete\",\"spread\":\"false\",\"parentId\":4,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":33,\"title\":\"新增\",\"icon\":null,\"href\":null,\"perms\":\"user:user:save\",\"spread\":\"false\",\"parentId\":10,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":34,\"title\":\"查看\",\"icon\":null,\"href\":null,\"perms\":\"user:user:list\",\"spread\":\"false\",\"parentId\":11,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":35,\"title\":\"新增\",\"icon\":null,\"href\":null,\"perms\":\"user:user:save\",\"spread\":\"false\",\"parentId\":11,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":36,\"title\":\"修改\",\"icon\":null,\"href\":null,\"perms\":\"user:user:update\",\"spread\":\"false\",\"parentId\":11,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":37,\"title\":\"删除\",\"icon\":null,\"href\":null,\"perms\":\"user:user:delete\",\"spread\":\"false\",\"parentId\":11,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":42,\"title\":\"查看\",\"icon\":\"\",\"href\":\"\",\"perms\":\"log:log:list\",\"spread\":\"false\",\"parentId\":15,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":44,\"title\":\"SQL监控\",\"icon\":\"&#xe642;\",\"href\":\"/druid\",\"perms\":null,\"spread\":\"false\",\"parentId\":43,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":45,\"title\":\"查看\",\"icon\":null,\"href\":null,\"perms\":\"sys:druid:list\",\"spread\":\"false\",\"parentId\":44,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":47,\"title\":\"查看\",\"icon\":null,\"href\":null,\"perms\":\"sys:menu:list\",\"spread\":\"false\",\"parentId\":46,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":48,\"title\":\"新增\",\"icon\":null,\"href\":null,\"perms\":\"sys:menu:save\",\"spread\":\"false\",\"parentId\":46,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":49,\"title\":\"修改\",\"icon\":null,\"href\":null,\"perms\":\"sys:menu:update\",\"spread\":\"false\",\"parentId\":46,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":50,\"title\":\"删除\",\"icon\":null,\"href\":null,\"perms\":\"sys:menu:delete\",\"spread\":\"false\",\"parentId\":46,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":52,\"title\":\"轮播管理\",\"icon\":\"\uE666\",\"href\":\"/carousel/carouselList\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":51,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":53,\"title\":\"查看\",\"icon\":\"\",\"href\":\"\",\"perms\":\"carousel:carousel:list\",\"spread\":\"false\",\"parentId\":52,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":54,\"title\":\"删除\",\"icon\":\"\",\"href\":\"\",\"perms\":\"carousel:carousel:delete\",\"spread\":\"false\",\"parentId\":52,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":55,\"title\":\"修改\",\"icon\":\"\",\"href\":\"\",\"perms\":\"carousel:carousel:update\",\"spread\":\"false\",\"parentId\":52,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":56,\"title\":\"新增\",\"icon\":\"\",\"href\":\"\",\"perms\":\"carousel:carousel:save\",\"spread\":\"false\",\"parentId\":52,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":62,\"title\":\"test\",\"icon\":\"\",\"href\":\"\",\"perms\":\"\",\"spread\":\"false\",\"parentId\":14,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":70,\"title\":\"oracle库\",\"icon\":\"\",\"href\":\"/dynamic/page/1\",\"perms\":\"dynamic:dynamic:view\",\"spread\":\"false\",\"parentId\":69,\"sorting\":null,\"checked\":null,\"isOpen\":false},{\"menuId\":72,\"title\":\"用户管理\",\"icon\":\"&#xe613;\",\"href\":\"/user/manager\",\"perms\":null,\"spread\":\"false\",\"parentId\":2,\"sorting\":null,\"checked\":null,\"isOpen\":false}]");
        VO vo = new VO();
        vo.setCount(1);
        vo.setCode(0);
        vo.setData(json);
        return vo;
    }

}
